package com.wfu.module_base.utils;

import android.app.Activity;

import java.util.ArrayList;
import java.util.List;

/**
 * Describe: 统一管理Activity生命周期
 */
public class ActivityManager {
    private static volatile ActivityManager mActivityManager;
    private List<Activity> mActivities;

    private ActivityManager() {
        if (mActivityManager != null)
            throw new RuntimeException("ActivityManager only an instance");
        mActivities = new ArrayList<>();
    }

    public static ActivityManager getAtcManager() {
        if (mActivityManager == null) {
            synchronized (ActivityManager.class) {
                mActivityManager = new ActivityManager();
            }
        }
        return mActivityManager;
    }

    /**
     * 添加Activity
     *
     * @param activity
     */
    public void addActivity(Activity activity) {
        if (activity != null) {
            mActivities.add(activity);
        }
    }

    /**
     * 移除Activity
     *
     * @param activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            activity.finish();
            mActivities.remove(activity);
        }
    }
}
